package com.quickfile.jrefilesystem;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import com.quickfile.File;
import com.quickfile.FileFilter;
import com.quickfile.FilenameFilter;

public class FileImpl extends File {

	public java.io.File file;

	public FileImpl(String filepath) {
		file = new java.io.File(filepath);
	}

	public FileImpl(String parent, String child) {
		file = new java.io.File(parent, child);
	}

	public FileImpl(File parent, String child) {
		file = new java.io.File(((FileImpl) parent).file, child);
	}

	public FileImpl(java.io.File file) {
		this.file = file;
	}

	@Override
	public boolean canRead() {
		return file.canRead();
	}

	@Override
	public boolean canWrite() {
		return file.canWrite();
	}

	@Override
	public int compareTo(File pathname) {
		return this.file.compareTo(((FileImpl) pathname).file);
	}

	@Override
	public boolean createNewFile() throws IOException {
		return file.createNewFile();
	}

	@Override
	public File createTempFile2(String prefix, String suffix)
			throws IOException {
		return new FileImpl(java.io.File.createTempFile(prefix, suffix));
	}

	@Override
	public File createTempFile2(String prefix, String suffix, File directory)
			throws IOException {
		return new FileImpl(java.io.File.createTempFile(prefix, suffix,
				((FileImpl) directory).file));
	}

	@Override
	public boolean delete() {
		return file.delete();
	}

	@Override
	public void deleteOnExit() {
		file.deleteOnExit();
	}

	@Override
	public boolean equals(Object obj) {
		return file.equals(obj);
	}

	@Override
	public boolean exists() {
		return file.exists();
	}

	@Override
	public File getAbsoluteFile() {
		return new FileImpl(file.getAbsoluteFile());
	}

	@Override
	public String getAbsolutePath() {
		return file.getAbsolutePath();
	}

	@Override
	public File getCanonicalFile() throws IOException {
		return new FileImpl(file.getCanonicalFile());
	}

	@Override
	public String getCanonicalPath() throws IOException {
		return file.getCanonicalPath();
	}

	@Override
	public String getName() {
		return file.getName();
	}

	@Override
	public String getParent() {
		return file.getParent();
	}

	@Override
	public File getParentFile() {
		return new FileImpl(file.getParentFile());
	}

	@Override
	public String getPath() {
		return file.getPath();
	}

	@Override
	public int hashCode() {
		return file.hashCode();
	}

	@Override
	public boolean isAbsolute() {
		return file.isAbsolute();
	}

	@Override
	public boolean isDirectory() {
		return file.isDirectory();
	}

	@Override
	public boolean isFile() {
		return file.isFile();
	}

	@Override
	public boolean isHidden() {
		return file.isHidden();
	}

	@Override
	public long lastModified() {
		return file.lastModified();
	}

	@Override
	public long length() {
		return file.length();
	}

	@Override
	public String[] list() {
		return file.list();
	}

	@Override
	public String[] list(final FilenameFilter filter) {
		if (filter == null)
			return file.list(null);

		java.io.FilenameFilter filter2 = new java.io.FilenameFilter() {

			@Override
			public boolean accept(java.io.File dir, String name) {
				return filter.accept(new FileImpl(dir), name);
			}
		};
		return file.list(filter2);
	}

	@Override
	public File[] listFiles() {
		return convertJavaFilesToFiles(file.listFiles());
	}

	@Override
	public File[] listFiles(final FileFilter filter) {
		java.io.FileFilter filter2 = null;
		if (filter != null) {
			filter2 = new java.io.FileFilter() {

				@Override
				public boolean accept(java.io.File pathname) {
					return filter.accept(new FileImpl(pathname));
				}

			};
		}
		return convertJavaFilesToFiles(file.listFiles(filter2));
	}

	@Override
	public File[] listFiles(final FilenameFilter filter) {
		java.io.FilenameFilter filter2 = null;
		if (filter != null) {
			filter2 = new java.io.FilenameFilter() {

				@Override
				public boolean accept(java.io.File dir, String name) {
					return filter.accept(new FileImpl(dir), name);
				}

			};
		}
		return convertJavaFilesToFiles(file.listFiles(filter2));
	}

	private File[] convertJavaFilesToFiles(java.io.File[] files) {
		if (files == null) {
			return null;
		}
		File[] files2 = new File[files.length];
		for (int i = 0; i < files.length; i++) {
			files2[i] = new FileImpl(files[i]);
		}
		return files2;
	}

	@Override
	public File[] listRoots2() {
		return convertJavaFilesToFiles(java.io.File.listRoots());
	}

	@Override
	public boolean mkdir() {
		return file.mkdir();
	}

	@Override
	public boolean mkdirs() {
		return file.mkdirs();
	}

	@Override
	public boolean renameTo(File dest) {
		return file.renameTo(((FileImpl) dest).file);
	}

	@Override
	public boolean setLastModified(long time) {
		return file.setLastModified(time);
	}

	@Override
	public boolean setReadOnly() {
		return file.setReadOnly();
	}

	@Override
	public String toString() {
		return file.toString();
	}

	@Override
	public URI toURI() {
		return file.toURI();
	}

	@Override
	public URL toURL() throws MalformedURLException {
		return file.toURL();
	}

}
